package com.mengdie.shuidi.ui.activity;

import android.os.Bundle;
import android.support.v4.app.FragmentTabHost;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TextView;

import com.mengdie.shuidi.R;
import com.mengdie.shuidi.manager.MDModel;
import com.mengdie.shuidi.ui.fragment.ProvinceLineFragment;
import com.mengdie.shuidi.ui.fragment.SeniorModelFragment;

/**
 * 作者：HQY on 17/8/5 16:46
 * 邮箱：hqy_xz@126.com
 */

public class LineSelectActivity extends BaseActivity {


    private FragmentTabHost mTabHost;
    private LayoutInflater layoutInflater;
    private TextView tabTv;
    private int mImageViewArray[] = {R.drawable.line_select_one, R.drawable.line_select_two};
    private final Class fragmentArray[] = {ProvinceLineFragment.class, SeniorModelFragment.class};

    private String mTextviewArray[];

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_line_select);
        mTextviewArray = getResources().getStringArray(R.array.line_select);
        mTabHost = (FragmentTabHost) findViewById(R.id.tabhost);
        layoutInflater = LayoutInflater.from(this);
        mTabHost.setup(this, getSupportFragmentManager(), R.id.contentPanel);

        int fragmentCount = fragmentArray.length;
        for (int i = 0; i < fragmentCount; i++) {
            //为每一个Tab按钮设置图标、文字和内容
            TabHost.TabSpec tabSpec = mTabHost.newTabSpec(mTextviewArray[i]).setIndicator(getTabItemView(i));
            //将Tab按钮添加进Tab选项卡中
            mTabHost.addTab(tabSpec, fragmentArray[i], null);
            mTabHost.getTabWidget().setDividerDrawable(null);
        }
        mTabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
            @Override
            public void onTabChanged(String s) {
                int num ;
                if (s.equals(mTextviewArray[0])) {
                    num = 0;
                } else {
                    num = 1;
                }
                MDModel.getInstance().setModel(num);
                MDModel.getInstance().writeToCache();

            }
        });
        mTabHost.setCurrentTab(MDModel.getInstance().getModel());
    }

    private View getTabItemView(int index) {
//        String pakageName = AppUtils.getAppPackageName(LineSelectActivity.this);
//        String temp[] = pakageName.split("\\.");
        View view;
        view = layoutInflater.inflate(R.layout.tab_content, null);
        ImageView icon = (ImageView) view.findViewById(R.id.tab_icon);
        tabTv = (TextView) view.findViewById(R.id.tab_tv);
        icon.setImageResource(mImageViewArray[index]);
        tabTv.setText(mTextviewArray[index]);
        return view;
    }

}
